<?php
namespace App\Repository;
use App\Exceptions\BusinessException;
use App\Common\ResponseCode;
use App\Models\Account\CustomerAccountRechargeLog;
use Illuminate\Support\Facades\Log;

/**
 * 客户账户数据仓储类
 *
 * @package App\Http\Repository
 * @author WangBin
 * @mail: <849850352@qq.com>
 */
class AccountRepository extends BaseRepository
{
    /**
     * 保存数据
     *
     * @param array $data
     * @param int $addressId
     * @throws
     * @return int|mixed
     */
    public function save(array $data)
    {
        try {
            $query = new CustomerAccountRechargeLog();
            collect($data)->each(function ($value, $field) use ($query) {
                $query->{$field} = $value;
            });
            return $query->save() ? $query->recharge_log_id : 0;
        } catch (\Exception $exception) {
            Log::error("[客户充值日志] 保存失败：{$exception->getMessage()}");
            throw new BusinessException(ResponseCode::SAVE_FAIL);
        }
    }

    /**
     * 充值记录列表
     *
     * @param array $filter
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getList(array $filter = [])
    {
        $query = CustomerAccountRechargeLog::query()->orderByDesc('created_at');
        if(!empty($filter['customer_id'])) {
            $query->where('customer_id', $filter['customer_id']);
        }
        return $query;
    }
}
